package api

import (
	"github.com/astaxie/beego"
)

type Api struct {
	Code    int         `json:"code"`
	Data    interface{} `json:"data"`
	Message string      `json:"message"`
	Count   int         `json:"count"`
}

type base struct {
	beego.Controller
}

type UploadApi struct {
	Success int    `json:"success"`
	Url     string `json:"url"`
	Message string `json:"message"`
}

var (
	uploadApi UploadApi
)

/**
@param message
*/
func (b *baseController) success(data interface{}, params ...interface{}) {
	var api Api
	api.Data = data
	api.Message = "操作成功"
	if len(params) > 0 {
		api.Message = params[0].(string)
		if len(params) > 1 {
			api.Count = params[1].(int)
		}
	}

	api.Code = 1
	b.baseReturn(api)
}

func (b *baseController) error(message string) {
	var api Api
	api.Message = message
	api.Code = 0
	b.baseReturn(api)
}

func (b *baseController) baseReturn(api Api) {
	b.Data["json"] = api
	b.ServeJSON()
	b.StopRun()
}

func (b *baseController) uploadError(message string) {
	uploadApi.Message = message
	uploadApi.Success = 0
	b.Data["json"] = uploadApi
	b.ServeJSON()
	b.StopRun()
}

func (b *baseController) uploadSuccess(url string) {
	uploadApi.Message = "上传成功"
	uploadApi.Success = 1
	uploadApi.Url = url
	b.Data["json"] = uploadApi
	b.ServeJSON()
	b.StopRun()
}
